package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.MyApp;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class UpLoginPsdActivity extends BaseActivity implements View.OnClickListener{
    private ImageView uploginPsd_back;
    private EditText uploginPsd_oldPsd,uploginPsd_newPsd,uploginPsd_configPsd;
    private TextView uploginPsd_sure;

    private ImageView uploginPsd_newPsd_img,uploginPsd_configPsd_img;

    private boolean flag1 = true;
    private boolean flag2 = true;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_up_loginpswd);
        initView();
    }
    private void initView(){
        uploginPsd_back = (ImageView) findViewById(R.id.uploginPsd_back);
        uploginPsd_back.setOnClickListener(this);
        uploginPsd_oldPsd = (EditText) findViewById(R.id.uploginPsd_oldPsd);
        uploginPsd_newPsd = (EditText) findViewById(R.id.uploginPsd_newPsd);
        uploginPsd_configPsd = (EditText) findViewById(R.id.uploginPsd_configPsd);
        uploginPsd_sure = (TextView) findViewById(R.id.uploginPsd_sure);
        uploginPsd_sure.setOnClickListener(this);
        uploginPsd_newPsd_img = (ImageView) findViewById(R.id.uploginPsd_newPsd_img);
        uploginPsd_newPsd_img.setOnClickListener(this);
        uploginPsd_configPsd_img = (ImageView) findViewById(R.id.uploginPsd_configPsd_img);
        uploginPsd_configPsd_img.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.uploginPsd_back:
                finish();
                break;
            case R.id.uploginPsd_sure:
                upLoginPsd();
                break;
            case R.id.uploginPsd_newPsd_img:
                if (flag1)
                {
                    uploginPsd_newPsd_img.setImageResource(R.mipmap.password_open);
                    uploginPsd_newPsd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag1 = false;
                }else {
                    uploginPsd_newPsd_img.setImageResource(R.mipmap.password_close);
                    uploginPsd_newPsd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag1 = true;
                }
                break;
            case R.id.uploginPsd_configPsd_img:
                if (flag2)
                {
                    uploginPsd_configPsd_img.setImageResource(R.mipmap.password_open);
                    uploginPsd_configPsd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    flag2 = false;
                }else {
                    uploginPsd_configPsd_img.setImageResource(R.mipmap.password_close);
                    uploginPsd_configPsd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    flag2 = true;
                }
                break;
        }
    }
    private void upLoginPsd(){
        String oldPsd = uploginPsd_oldPsd.getText().toString().trim();
        if (oldPsd.isEmpty()){
            showToast("请输入原密码");
            return;
        }
        String newPsd = uploginPsd_newPsd.getText().toString().trim();
        if (newPsd.isEmpty()){
            showToast("请输入新密码");
            return;
        }
        if (newPsd.length()<6){
            showToast("密码不少于6位");
            return;
        }
        String configPsd = uploginPsd_configPsd.getText().toString().trim();
        if (!newPsd.equals(configPsd)){
            showToast("密码输入不一致");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("token", MyApp.userData.getData().getToken());
        map.put("user",MyApp.userData.getData().getUser_id());
        map.put("passwd_old",oldPsd);
        map.put("passwd_new",newPsd);
        XUtil.Post(UrlConfig.UP_LOGINPSD_URL,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                MyLogger.wLog().e("修改密码result="+result);
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("code")==200){
                    UpLoginPsdActivity.this.finish();
                }
                showToast(jsonObject.getString("msg"));
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                MyLogger.wLog().e("修改密码失败");
            }
        });
    }

}
